﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bet_Yatziv
{
    public partial class Warehouse : Form
    {     
        public Warehouse()
        {
            InitializeComponent();
            refresh();
        }

        //closing window
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //clearing form
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            clearAll();
        }

        private void Badd_Click(object sender, EventArgs e)
        {
            DataTable table = DataAccessLayer.GetAllWarehouse();
            dataGridView1.DataSource = table;  
        }

        //converting to int
        private int convert(ComboBox box, string text, string error)
        {
            int ans = -1;
            if (text.Length > 0)
            {
                try
                {
                    ans = Convert.ToInt32(text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "" + error + "", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    box.Text = "";
                }
            }
            return ans;
        }


        //converting to int
        private int convert(TextBox box, string text, string error)
        {
            int ans = -1;
            if (text.Length > 0)
            {
                try
                {
                    ans = Convert.ToInt32(text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "" + error + "", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    box.Text = "";
                }
            }
            return ans;
        }

        //clearing all text boxes
        public void clearAll()
        {
            txt_WitemName.Text = txt_WitemCode.Text = txt_itemCode.Text = txt_Wamount.Text = "";
            txt_itemName.Text =  txt_amount.Text =  "";
        }

        //refreshing all data
        public void refresh()
        {
            txt_itemCode.Items.Clear();
            txt_WitemCode.Items.Clear();

            //bringing all the item details
            DataTable cost = DataAccessLayer.GetAllWarehouse();
            string query2 = "select itemID from tbl_warehouse";
            DataTable dt2 = AdoHelper.ExecuteDataTable(query2);
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                txt_itemCode.Items.Add(dt2.Rows[i].ItemArray[0]);
                txt_WitemCode.Items.Add(dt2.Rows[i].ItemArray[0]);
            }

            clearAll();
            dataGridView1.DataSource = DataAccessLayer.GetAllWarehouse();
        }

        private void txt_itemCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            string query1 = "select * from tbl_warehouse";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            int itemCodeNum = 0;
            if (txt_itemCode.Text.Length > 0)
            {
                try
                {
                    itemCodeNum = Convert.ToInt32(txt_itemCode.Text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "אין אפשרות להמיר את ערך קוד הפריט", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_itemCode.Text = "";
                    return;
                }
            }

            for (i = 0; i < dt1.Rows.Count; i++)
                if (itemCodeNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            txt_itemName.Text = "" + dt1.Rows[i].ItemArray[1];
            txt_amount.Text = "" + dt1.Rows[i].ItemArray[2];
        }

        private void txt_WitemCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            string query1 = "select * from tbl_warehouse";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            int itemCodeNum = 0;
            if (txt_WitemCode.Text.Length > 0)
            {
                try
                {
                    itemCodeNum = Convert.ToInt32(txt_WitemCode.Text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "אין אפשרות להמיר את ערך קוד הפריט", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_WitemCode.Text = "";
                    return;
                }
            }

            for (i = 0; i < dt1.Rows.Count; i++)
                if (itemCodeNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            txt_WitemName.Text = "" + dt1.Rows[i].ItemArray[1];
        }

        private void mainUpdate_Click(object sender, EventArgs e)
        {
            string query = "SELECT tbl_warehouse.itemID, tbl_warehouse.description, tbl_warehouse.amount, tbl_warehouse.minAmount FROM tbl_warehouse WHERE (((tbl_warehouse.amount)<[tbl_warehouse].[minAmount]))";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query);
            dataGridView1.DataSource = dt1;
            
        }
    }
}